<template>
  <div class="header">
    <span class="header-span">鱿鱼溪学院后台管理系统 V1.0</span>
  </div>
  <div class="loginWraper">
    <div id="loginform" class="loginBox">
      <el-form
        ref="ruleFormRef"
        :model="form"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
        style="margin: 20px 0"
      >
        <el-form-item prop="username">
          <svg-icon
            name="account"
            width="26px"
            height="26px"
            class="svg-icon"
          ></svg-icon>
          <el-input
            v-model="form.username"
            type="text"
            style="width: 80%; height: 40px"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="password" style="margin-top: 28px">
          <svg-icon
            name="password"
            class="svg-icon"
            width="26px"
            height="26px"
          ></svg-icon>
          <el-input
            v-model="form.password"
            type="password"
            show-password
            style="width: 80%; height: 40px"
          />
        </el-form-item>
        <el-form-item style="margin-left: 34px; margin-top: 32px">
          <el-button
            type="primary"
            @click="submitForm(ruleFormRef)"
            :loading="isLoading"
            >&nbsp;登&nbsp;&nbsp;&nbsp;&nbsp;录&nbsp;</el-button
          >
          <el-button @click="resetForm(ruleFormRef)"
            >&nbsp;重&nbsp;&nbsp;置&nbsp;</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
  <div class="footer">Copyright 鱿鱼溪学院 by edu v1.0</div>
</template>

<script setup lang="ts">
//import Schema from 'async-validator'

import { loginFormData } from '@/api/user/type'

import { ElNotification, type FormInstance, type FormRules } from 'element-plus'
import { reactive, ref } from 'vue'

import { useRoute, useRouter } from 'vue-router'
let $router = useRouter()
// import { errorMessage } from '@/api/user/type'
//引入用户相关的小仓库
import useUserStore from '@/store/modules/user2'

//引入layer弹窗插件
import { layer } from '@layui/layer-vue'
import getTime from '@/utils/time'
import { setTimeStamp } from '@/utils/auth'

//import getTime from '@/utils/time'

//获取路由器
//let $router = useRouter()
//路由对象

let $route = useRoute()

//组件实例对象
const ruleFormRef = ref<FormInstance>()

//表单数据
const form = reactive<loginFormData>({
  username: '',
  password: ''
})

const rules = reactive<FormRules>({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: '请输入用户名！'
    },
    {
      min: 3,
      max: 18,
      message: '用户名须为3到18位！',
      trigger: 'blur'
      //informType: 'warning'
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      message: '请输入密码！'
    },
    {
      min: 6,
      max: 18,
      message: '密码须为6到18位！',
      trigger: 'blur'
      //  informType: 'warning',
    }
  ]
})

//定义是否登录加载中
const isLoading = ref(false)
//表单提交
const submitForm = async (ruleFormRef: FormInstance | undefined) => {
  isLoading.value = true
  if (!ruleFormRef) return
  await ruleFormRef.validate(async (valid: boolean) => {
    if (!valid) {
      isLoading.value = false
      return
    }
    console.log(' 校验通过,可以发起请求')

    try {
      //也可以写.then

      let userStore = useUserStore()

      let res = await userStore.login(form)
      // console.log(res, 'user2')
      // let res = await store.login(form)
      if (res) {
        isLoading.value = false

        //判断登录的时候，路由路径是否有query参数，如果有就往query参数跳转，没有跳转到首页
        let redirect: any = $route.query.redirect

        //跳转
        $router.push({ path: redirect || '/' })

        //保存登录时间
        setTimeStamp()

        //layer.msg('登录成功，加载页面。。。', { icon: 16, time: 1000 })

        ElNotification({
          type: 'success',
          message: '欢迎回来！',
          title: `Hi,${getTime()}好`
        })
      }
      //登录成功后加载效果消失
      isLoading.value = false
    } catch (error: any) {
      console.log(error)
      //登录失败，加载效果消失
      isLoading.value = false
      ElNotification({
        type: 'error',
        message: error.msg
      })
      return
      // throw new Error(error.msg)
    }
  })
}

//重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped lang="scss">
.header {
  top: 0;
  height: 60px;
  line-height: 60px;
  background: #426374 no-repeat 0 center;
}

.header-span {
  color: #fff;
  font-size: 32px;
  font-style: italic;
  align-items: center;
  margin-left: 10px;
  // font-family: Arial, Helvetica, sans-serif;
}
.header,
.footer {
  position: absolute;
  left: 0;
  right: 0;
  width: 100%;
  z-index: 99;
}
.loginWraper {
  position: absolute;
  width: 100%;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
  background: #3283ac url(@/assets/images/admin-login-bg.jpg) no-repeat center;
}
.loginBox {
  position: absolute;
  width: 617px;
  height: 330px;
  background: url(@/assets/images/admin-loginform-bg.png) no-repeat;
  left: 50%;
  top: 50%;
  margin-left: -309px;
  margin-top: -184px;
  padding-top: 38px;
}
.loginBox .input {
  width: 360px;
  border: 1px solid #ddd;
  display: inline-block;
  -webkit-transition: all 0.2s linear 0s;
  -moz-transition: all 0.2s linear 0s;
  -o-transition: all 0.2s linear 0s;
  transition: all 0.2s linear 0s;
}
input[type='text']:focus {
  outline: none;
}
input[type='password']:focus {
  outline: none;
}

.form-horizontal {
  margin-left: 140px;
  margin-top: 30px;
}
.form-horizontal .form-label {
  margin-top: 3px;
  text-align: right;
  margin-right: 5px;
}
.password {
  margin-top: 20px;
}
.button-area {
  margin-left: 30px;
  margin-top: 30px;
}
.input.size-L {
  font-size: 16px;
  padding: 8px;
  height: 24px;
}
.size-btn {
  padding-left: 15px;
  margin-top: 40px;
  padding-right: 15px;
  padding-top: 8px;
  padding-bottom: 8px;
  margin-right: 10px;
}

:deep(.el-form-item__error) {
  margin-left: 28px;
}

.radius {
  border-radius: 4px;
}
//这里对图标组件进行了相对定位，进行了微调
.svg-icon {
  display: inline-block;
  margin-right: 2px;
  position: relative;
  top: 5px;
  right: 10px;
  cursor: pointer;
}
/* 按钮样式 */
// .btn {
//   display: inline-block;
//   box-sizing: border-box;
//   cursor: pointer;
//   text-align: center;
//   font-weight: 400;
//   white-space: nowrap;
//   vertical-align: middle;
//   -moz-padding-start: npx;
//   -moz-padding-end: npx;
//   border: solid 1px #ddd;
//   background-color: #fff;
//   width: auto;
//   *zoom: 1;
//   *overflow: visible;
//   -webkit-transition: background-color 0.1s linear;
//   -moz-transition: background-color 0.1s linear;
//   -o-transition: background-color 0.1s linear;
//   transition: background-color 0.1s linear;
//   font-size: 15px;
// }

// .btn-success {
//   color: #fff;
//   background-color: #5eb95e;
//   border-color: #5eb95e;
// }

// .btn-default {
//   background-color: #e6e6e6;
//   border-color: #e6e6e6;
// }

.footer {
  height: 46px;
  line-height: 46px;
  bottom: 0;
  text-align: center;
  color: #fff;
  font-size: 12px;
  background-color: #426374;
  font-style: italic;
}
.check-message {
  color: #d03050;
  position: relative;
  left: 30px;
  top: 8px;
}

.checkusername,
.checkpassword {
  border: 2px solid #d03050 !important ;
}
</style>
